package com.four.hospital.service.impl;

import com.four.hospital.dao.AreaDao;
import com.four.hospital.entity.Area;
import com.four.hospital.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author:wsy
 * @Date: 2022/01/12/10:49
 * @Description
 */
@Service
public class AreaServiceImpl implements AreaService {

    @Autowired
    private AreaDao areaDao;

    /**
     * 查所有地址
     * @return
     */
    @Override
    public List<Area> threeArea() {
        //查询所有地址
        List<Area> allArea=areaDao.allArea();
        //筛选出省
        List<Area> areaListOne=allArea.stream().filter(item ->{
            return item.getAreaPid().equals(0);
        }).map(item -> {
            //每一个item就是一个分类
            //设置二级分类
            //获取二级分类
            item.setAreaChildren(getChildren(item.getAreaId(),allArea));
            return item;
        }).collect(Collectors.toList());
        return areaListOne;
    }

    private List<Area> getChildren(Integer areaId,List<Area> allArea) {
        List<Area> child=allArea.stream().filter(item ->{
            return item.getAreaPid().equals(areaId);
        }).map(item ->{
            //item就是二级分类
            item.setAreaChildren(getChildren(item.getAreaId(),allArea));
            return item;
        }).collect(Collectors.toList());
        return child;
    }
}
